home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpdfx.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  25.7 KB  |  769 lines

  1. /* Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpdfx.h,v 1.22 2000/09/19 19:00:17 lpd Exp $ */
  20. /* Internal definitions for PDF-writing driver. */
  21.  
  22. #ifndef gdevpdfx_INCLUDED
  23. #  define gdevpdfx_INCLUDED
  24.  
  25. #include "gsparam.h"
  26. #include "gsuid.h"
  27. #include "gxdevice.h"
  28. #include "gxfont.h"
  29. #include "gxline.h"
  30. #include "stream.h"
  31. #include "spprint.h"
  32. #include "gdevpsdf.h"
  33.  
  34. /* ---------------- Statically allocated sizes ---------------- */
  35. /* These should really be dynamic.... */
  36.  
  37. /* Define the maximum depth of an outline tree. */
  38. /* Note that there is no limit on the breadth of the tree. */
  39. #define MAX_OUTLINE_DEPTH 8
  40.  
  41. /* Define the maximum size of a destination array string. */
  42. #define MAX_DEST_STRING 80
  43.  
  44. /* ================ Types and structures ================ */
  45.  
  46. /* Define the possible contexts for the output stream. */
  47. typedef enum {
  48.     PDF_IN_NONE,
  49.     PDF_IN_STREAM,
  50.     PDF_IN_TEXT,
  51.     PDF_IN_STRING
  52. } pdf_context_t;
  53.  
  54. /* ---------------- Cos objects ---------------- */
  55.  
  56. /*
  57.  * These are abstract types for cos objects.  The concrete types are in
  58.  * gdevpdfo.h.
  59.  */
  60. typedef struct cos_object_s cos_object_t;
  61. typedef struct cos_stream_s cos_stream_t;
  62. typedef struct cos_dict_s cos_dict_t;
  63. typedef struct cos_array_s cos_array_t;
  64. typedef struct cos_value_s cos_value_t;
  65. typedef struct cos_object_procs_s cos_object_procs_t;
  66. typedef const cos_object_procs_t *cos_type_t;
  67. #define cos_types_DEFINED
  68.  
  69. /* ---------------- Resources ---------------- */
  70.  
  71. typedef enum {
  72.     /*
  73.      * Standard PDF resources.  Font must be last, because resources
  74.      * up to but not including Font are written page-by-page.
  75.      */
  76.     resourceColorSpace,
  77.     resourceExtGState,
  78.     resourcePattern,
  79.     resourceShading,
  80.     resourceXObject,
  81.     resourceFont,
  82.     /*
  83.      * Internally used (pseudo-)resources.
  84.      */
  85.     resourceCharProc,
  86.     resourceFontDescriptor,
  87.     resourceFunction,
  88.     NUM_RESOURCE_TYPES
  89. } pdf_resource_type_t;
  90.  
  91. #define pdf_resource_type_names\
  92.   "ColorSpace", "ExtGState", "Pattern", "Shading", "XObject", "Font",\
  93.   0, 0, 0
  94. #define pdf_resource_type_structs\
  95.   &st_pdf_resource,        /* see below */\
  96.   &st_pdf_resource,\
  97.   &st_pdf_resource,\
  98.   &st_pdf_resource,\
  99.   &st_pdf_x_object,        /* see below */\
  100.   &st_pdf_font,            /* gdevpdff.h / gdevpdff.c */\
  101.   &st_pdf_char_proc,        /* gdevpdff.h / gdevpdff.c */\
  102.   &st_pdf_font_descriptor,    /* gdevpdff.h / gdevpdff.c */\
  103.   &st_pdf_resource
  104.  
  105. #define pdf_resource_common(typ)\
  106.     typ *next;            /* next resource of this type */\
  107.     pdf_resource_t *prev;    /* previously allocated resource */\
  108.     gs_id rid;            /* optional ID key */\
  109.     bool named;\
  110.     bool used_on_page;\
  111.     cos_object_t *object
  112. typedef struct pdf_resource_s pdf_resource_t;
  113. struct pdf_resource_s {
  114.     pdf_resource_common(pdf_resource_t);
  115. };
  116.  
  117. /* The descriptor is public for subclassing. */
  118. extern_st(st_pdf_resource);
  119. #define public_st_pdf_resource()  /* in gdevpdfu.c */\
  120.   gs_public_st_ptrs3(st_pdf_resource, pdf_resource_t, "pdf_resource_t",\
  121.     pdf_resource_enum_ptrs, pdf_resource_reloc_ptrs, next, prev, object)
  122.  
  123. /*
  124.  * We define XObject resources here because they are used for Image,
  125.  * Form, and PS XObjects, which are implemented in different files.
  126.  */
  127. typedef struct pdf_x_object_s pdf_x_object_t;
  128. struct pdf_x_object_s {
  129.     pdf_resource_common(pdf_x_object_t);
  130.     int width, height;        /* specified width and height for images */
  131.     int data_height;        /* actual data height for images */
  132. };
  133. #define private_st_pdf_x_object()  /* in gdevpdfu.c */\
  134.   gs_private_st_suffix_add0(st_pdf_x_object, pdf_x_object_t,\
  135.     "pdf_x_object_t", pdf_x_object_enum_ptrs, pdf_x_object_reloc_ptrs,\
  136.     st_pdf_resource)
  137.  
  138. /* ------ Fonts ------ */
  139.  
  140. /* Define the standard fonts. */
  141. #define PDF_NUM_STD_FONTS 14
  142. #define pdf_do_std_fonts(m)\
  143.   m("Courier", ENCODING_INDEX_STANDARD)\
  144.   m("Courier-Bold", ENCODING_INDEX_STANDARD)\
  145.   m("Courier-Oblique", ENCODING_INDEX_STANDARD)\
  146.   m("Courier-BoldOblique", ENCODING_INDEX_STANDARD)\
  147.   m("Helvetica", ENCODING_INDEX_STANDARD)\
  148.   m("Helvetica-Bold", ENCODING_INDEX_STANDARD)\
  149.   m("Helvetica-Oblique", ENCODING_INDEX_STANDARD)\
  150.   m("Helvetica-BoldOblique", ENCODING_INDEX_STANDARD)\
  151.   m("Symbol", ENCODING_INDEX_SYMBOL)\
  152.   m("Times-Roman", ENCODING_INDEX_STANDARD)\
  153.   m("Times-Bold", ENCODING_INDEX_STANDARD)\
  154.   m("Times-Italic", ENCODING_INDEX_STANDARD)\
  155.   m("Times-BoldItalic", ENCODING_INDEX_STANDARD)\
  156.   m("ZapfDingbats", ENCODING_INDEX_DINGBATS)
  157.  
  158. /* Define the range of synthesized space characters. */
  159. #define X_SPACE_MIN 24
  160. #define X_SPACE_MAX 150
  161.  
  162. /* Define abstract types.  The concrete types are in gdevpdff.h. */
  163. typedef struct pdf_font_s pdf_font_t;
  164.  
  165. /* ------ Named objects ------ */
  166.  
  167. /* Define an element of the graphics object accumulation (BP/EP) stack. */
  168. typedef struct pdf_graphics_save_s pdf_graphics_save_t;
  169. struct pdf_graphics_save_s {
  170.     pdf_graphics_save_t *prev;
  171.     cos_stream_t *object;
  172.     long position;
  173.     pdf_context_t save_context;
  174.     long save_contents_id;
  175. };
  176.  
  177. #define private_st_pdf_graphics_save()    /* in gdevpdfm.c */\
  178.   gs_private_st_ptrs2(st_pdf_graphics_save, pdf_graphics_save_t,\
  179.     "pdf_graphics_save_t", pdf_graphics_save_enum_ptrs,\
  180.     pdf_graphics_save_reloc_ptrs, prev, object)
  181.  
  182. /* ---------------- Other auxiliary structures ---------------- */
  183.  
  184. /* Outline nodes and levels */
  185. typedef struct pdf_outline_node_s {
  186.     long id, parent_id, prev_id, first_id, last_id;
  187.     int count;
  188.     cos_dict_t *action;
  189. } pdf_outline_node_t;
  190. typedef struct pdf_outline_level_s {
  191.     pdf_outline_node_t first;
  192.     pdf_outline_node_t last;
  193.     int left;
  194. } pdf_outline_level_t;
  195. /*
  196.  * The GC descriptor is implicit, since outline levels occur only in an
  197.  * embedded array in the gx_device_pdf structure.
  198.  */
  199.  
  200. /* Articles */
  201. typedef struct pdf_bead_s {
  202.     long id, article_id, prev_id, next_id, page_id;
  203.     gs_rect rect;
  204. } pdf_bead_t;
  205. typedef struct pdf_article_s pdf_article_t;
  206. struct pdf_article_s {
  207.     pdf_article_t *next;
  208.     cos_dict_t *contents;
  209.     pdf_bead_t first;
  210.     pdf_bead_t last;
  211. };
  212.  
  213. #define private_st_pdf_article()\
  214.   gs_private_st_ptrs2(st_pdf_article, pdf_article_t,\
  215.     "pdf_article_t", pdf_article_enum_ptrs, pdf_article_reloc_ptrs,\
  216.     next, contents)
  217.  
  218. /* ---------------- The device structure ---------------- */
  219.  
  220. /* Text state */
  221. #ifndef pdf_font_descriptor_DEFINED
  222. #  define pdf_font_descriptor_DEFINED
  223. typedef struct pdf_font_descriptor_s pdf_font_descriptor_t;
  224. #endif
  225. typedef struct pdf_std_font_s {
  226.     gs_font *font;        /* weak pointer, may be 0 */
  227.     pdf_font_descriptor_t *pfd;  /* *not* a weak pointer */
  228.     gs_matrix orig_matrix;
  229.     gs_uid uid;            /* UniqueID, not XUID */
  230. } pdf_std_font_t;
  231. typedef struct pdf_text_state_s {
  232.     /* State parameters */
  233.     float character_spacing;
  234.     pdf_font_t *font;
  235.     floatp size;
  236.     float word_spacing;
  237.     float leading;
  238.     bool use_leading;        /* true => use ', false => use Tj */
  239.     /* Bookkeeping */
  240.     gs_matrix matrix;        /* relative to device space, not user space */
  241.     gs_point line_start;
  242.     gs_point current;
  243. #define max_text_buffer 200    /* arbitrary, but overflow costs 5 chars */
  244.     byte buffer[max_text_buffer];
  245.     int buffer_count;
  246. } pdf_text_state_t;
  247.  
  248. #define pdf_text_state_default\
  249.   0, NULL, 0, 0, 0, 0 /*false*/,\
  250.   { identity_matrix_body }, { 0, 0 }, { 0, 0 }, { 0 }, 0
  251.  
  252. /* Resource lists */
  253. #define NUM_RESOURCE_CHAINS 16
  254. typedef struct pdf_resource_list_s {
  255.     pdf_resource_t *chains[NUM_RESOURCE_CHAINS];
  256. } pdf_resource_list_t;
  257.  
  258. /* Define the hash function for gs_ids. */
  259. #define gs_id_hash(rid) ((rid) + ((rid) / NUM_RESOURCE_CHAINS))
  260. /* Define the accessor for the proper hash chain. */
  261. #define PDF_RESOURCE_CHAIN(pdev, type, rid)\
  262.   (&(pdev)->resources[type].chains[gs_id_hash(rid) % NUM_RESOURCE_CHAINS])
  263.  
  264. /* Define the bookkeeping for an open stream. */
  265. typedef struct pdf_stream_position_s {
  266.     long length_id;
  267.     long start_pos;
  268. } pdf_stream_position_t;
  269.  
  270. /* Define the mask for which procsets have been used on a page. */
  271. typedef enum {
  272.     NoMarks = 0,
  273.     ImageB = 1,
  274.     ImageC = 2,
  275.     ImageI = 4,
  276.     Text = 8
  277. } pdf_procset;
  278.  
  279. /*
  280.  * Define the structure for keeping track of text rotation.
  281.  * There is one for the current page (for AutoRotate /PageByPage)
  282.  * and one for the whole document (for AutoRotate /All).
  283.  */
  284. typedef struct pdf_text_rotation_s {
  285.     long counts[5];        /* 0, 90, 180, 270, other */
  286.     int Rotate;            /* computed rotation, -1 means none */
  287. } pdf_text_rotation_t;
  288. #define pdf_text_rotation_angle_values 0, 90, 180, 270, -1
  289.  
  290. /*
  291.  * Define the stored information for a page.  Because pdfmarks may add
  292.  * information to any page anywhere in the document, we have to wait
  293.  * until the end to write out the page dictionaries.
  294.  */
  295. typedef struct pdf_page_s {
  296.     cos_dict_t *Page;
  297.     gs_int_point MediaBox;
  298.     pdf_procset procsets;
  299.     long contents_id;
  300.     long resource_ids[resourceFont]; /* resources up to Font, see above */
  301.     long fonts_id;
  302.     cos_array_t *Annots;
  303.     pdf_text_rotation_t text_rotation;
  304. } pdf_page_t;
  305. #define private_st_pdf_page()    /* in gdevpdf.c */\
  306.   gs_private_st_ptrs2(st_pdf_page, pdf_page_t, "pdf_page_t",\
  307.     pdf_page_enum_ptrs, pdf_page_reloc_ptrs, Page, Annots)
  308.  
  309. /*
  310.  * Define the structure for the temporary files used while writing.
  311.  * There are 4 of these, described below.
  312.  */
  313. typedef struct pdf_temp_file_s {
  314.     char file_name[gp_file_name_sizeof];
  315.     FILE *file;
  316.     stream *strm;
  317.     byte *strm_buf;
  318.     stream *save_strm;        /* save pdev->strm while writing here */
  319. } pdf_temp_file_t;
  320.  
  321. /* Define the device structure. */
  322. #ifndef gx_device_pdf_DEFINED
  323. #  define gx_device_pdf_DEFINED
  324. typedef struct gx_device_pdf_s gx_device_pdf;
  325. #endif
  326. struct gx_device_pdf_s {
  327.     gx_device_psdf_common;
  328.     /* PDF-specific distiller parameters */
  329.     double CompatibilityLevel;
  330.     int EndPage;
  331.     int StartPage;
  332.     bool Optimize;
  333.     bool ParseDSCCommentsForDocInfo;
  334.     bool ParseDSCComments;
  335.     bool EmitDSCWarnings;
  336.     bool CreateJobTicket;
  337.     bool PreserveEPSInfo;
  338.     bool AutoPositionEPSFiles;
  339.     bool PreserveCopyPage;
  340.     bool UsePrologue;
  341.     /* End of distiller parameters */
  342.     /* Other parameters */
  343.     bool ReAssignCharacters;
  344.     bool ReEncodeCharacters;
  345.     long FirstObjectNumber;
  346.     /* End of parameters */
  347.     /* Additional graphics state */
  348.     bool fill_overprint, stroke_overprint;
  349.     int overprint_mode;
  350.     gs_id halftone_id;
  351.     gs_id transfer_ids[4];
  352.     gs_id black_generation_id, undercolor_removal_id;
  353.     /* Following are set when device is opened. */
  354.     enum {
  355.     pdf_compress_none,
  356.     pdf_compress_LZW,    /* not currently used, thanks to Unisys */
  357.     pdf_compress_Flate
  358.     } compression;
  359. #define pdf_memory v_memory
  360.     /*
  361.      * The xref temporary file is logically an array of longs.
  362.      * xref[id - FirstObjectNumber] is the position in the output file
  363.      * of the object with the given id.
  364.      *
  365.      * Note that xref, unlike the other temporary files, does not have
  366.      * an associated stream or stream buffer.
  367.      */
  368.     pdf_temp_file_t xref;
  369.     /*
  370.      * asides holds resources and other "aside" objects.  It is
  371.      * copied verbatim to the output file at the end of the document.
  372.      */
  373.     pdf_temp_file_t asides;
  374.     /*
  375.      * streams holds data for stream-type Cos objects.  The data is
  376.      * copied to the output file at the end of the document.
  377.      *
  378.      * Note that streams.save_strm is not used, since we don't interrupt
  379.      * normal output when saving stream data.
  380.      */
  381.     pdf_temp_file_t streams;
  382.     /*
  383.      * pictures holds graphic objects being accumulated between BP and EP.
  384.      * The object is moved to streams when the EP is reached: since BP and
  385.      * EP nest, we delete the object from the pictures file at that time.
  386.      */
  387.     pdf_temp_file_t pictures;
  388.     pdf_font_t *open_font;    /* current Type 3 synthesized font */
  389.     bool use_open_font;        /* if false, start new open_font */
  390.     long embedded_encoding_id;
  391.     /* ................ */
  392.     long next_id;
  393.     /* The following 3 objects, and only these, are allocated */
  394.     /* when the file is opened. */
  395.     cos_dict_t *Catalog;
  396.     cos_dict_t *Info;
  397.     cos_dict_t *Pages;
  398. #define pdf_num_initial_ids 3
  399.     long outlines_id;
  400.     int next_page;
  401.     long contents_id;
  402.     pdf_context_t context;
  403.     long contents_length_id;
  404.     long contents_pos;
  405.     pdf_procset procsets;    /* used on this page */
  406.     pdf_text_state_t text;
  407.     pdf_std_font_t std_fonts[PDF_NUM_STD_FONTS];
  408.     long space_char_ids[X_SPACE_MAX - X_SPACE_MIN + 1];
  409.     pdf_text_rotation_t text_rotation;
  410. #define initial_num_pages 50
  411.     pdf_page_t *pages;
  412.     int num_pages;
  413.     pdf_resource_list_t resources[NUM_RESOURCE_TYPES];
  414.     /* cs_Patterns[0] is colored; 1,3,4 are uncolored + Gray,RGB,CMYK */
  415.     pdf_resource_t *cs_Patterns[5];
  416.     pdf_resource_t *last_resource;
  417.     pdf_outline_level_t outline_levels[MAX_OUTLINE_DEPTH];
  418.     int outline_depth;
  419.     int closed_outline_depth;
  420.     int outlines_open;
  421.     pdf_article_t *articles;
  422.     cos_dict_t *Dests;
  423.     cos_dict_t *named_objects;
  424.     pdf_graphics_save_t *open_graphics;
  425. };
  426.  
  427. #define is_in_page(pdev)\
  428.   ((pdev)->contents_id != 0)
  429. #define is_in_document(pdev)\
  430.   (is_in_page(pdev) || (pdev)->last_resource != 0)
  431.  
  432. /* Enumerate the individual pointers in a gx_device_pdf. */
  433. #define gx_device_pdf_do_ptrs(m)\
  434.  m(0,asides.strm) m(1,asides.strm_buf) m(2,asides.save_strm)\
  435.  m(3,streams.strm) m(4,streams.strm_buf)\
  436.  m(5,pictures.strm) m(6,pictures.strm_buf) m(7,pictures.save_strm)\
  437.  m(8,open_font)\
  438.  m(9,Catalog) m(10,Info) m(11,Pages)\
  439.  m(12,text.font) m(13,pages)\
  440.  m(14,cs_Patterns[0])\
  441.  m(15,cs_Patterns[1]) m(16,cs_Patterns[3]) m(17,cs_Patterns[4])\
  442.  m(18,last_resource)\
  443.  m(19,articles) m(20,Dests) m(21,named_objects) m(22,open_graphics)
  444. #define gx_device_pdf_num_ptrs 23
  445. #define gx_device_pdf_do_strings(m) /* do nothing */
  446. #define gx_device_pdf_num_strings 0
  447. #define st_device_pdf_max_ptrs\
  448.   (st_device_psdf_max_ptrs + gx_device_pdf_num_ptrs +\
  449.    gx_device_pdf_num_strings +\
  450.    PDF_NUM_STD_FONTS * 2 /* std_fonts[].{font,pfd} */ +\
  451.    NUM_RESOURCE_TYPES * NUM_RESOURCE_CHAINS /* resources[].chains[] */ +\
  452.    MAX_OUTLINE_DEPTH * 2 /* outline_levels[].{first,last}.action */
  453.  
  454. #define private_st_device_pdfwrite()    /* in gdevpdf.c */\
  455.   gs_private_st_composite_final(st_device_pdfwrite, gx_device_pdf,\
  456.     "gx_device_pdf", device_pdfwrite_enum_ptrs, device_pdfwrite_reloc_ptrs,\
  457.     device_pdfwrite_finalize)
  458.  
  459. /* ================ Driver procedures ================ */
  460.  
  461.     /* In gdevpdfb.c */
  462. dev_proc_copy_mono(gdev_pdf_copy_mono);
  463. dev_proc_copy_color(gdev_pdf_copy_color);
  464. dev_proc_fill_mask(gdev_pdf_fill_mask);
  465. dev_proc_strip_tile_rectangle(gdev_pdf_strip_tile_rectangle);
  466.     /* In gdevpdfd.c */
  467. dev_proc_fill_rectangle(gdev_pdf_fill_rectangle);
  468. dev_proc_fill_path(gdev_pdf_fill_path);
  469. dev_proc_stroke_path(gdev_pdf_stroke_path);
  470.     /* In gdevpdfi.c */
  471. dev_proc_begin_typed_image(gdev_pdf_begin_typed_image);
  472.     /* In gdevpdfp.c */
  473. dev_proc_get_params(gdev_pdf_get_params);
  474. dev_proc_put_params(gdev_pdf_put_params);
  475.     /* In gdevpdft.c */
  476. dev_proc_text_begin(gdev_pdf_text_begin);
  477.  
  478. /* ================ Utility procedures ================ */
  479.  
  480. /* ---------------- Exported by gdevpdf.c ---------------- */
  481.  
  482. /* Initialize the IDs allocated at startup. */
  483. void pdf_initialize_ids(P1(gx_device_pdf * pdev));
  484.  
  485. /* Update the color mapping procedures after setting ProcessColorModel. */
  486. void pdf_set_process_color_model(P1(gx_device_pdf * pdev));
  487.  
  488. /* Reset the text state parameters to initial values. */
  489. void pdf_reset_text(P1(gx_device_pdf *pdev));
  490.  
  491. /* ---------------- Exported by gdevpdfu.c ---------------- */
  492.  
  493. /* ------ Document ------ */
  494.  
  495. /* Open the document if necessary. */
  496. void pdf_open_document(P1(gx_device_pdf * pdev));
  497.  
  498. /* ------ Objects ------ */
  499.  
  500. /* Allocate an ID for a future object. */
  501. long pdf_obj_ref(P1(gx_device_pdf * pdev));
  502.  
  503. /* Read the current position in the output stream. */
  504. long pdf_stell(P1(gx_device_pdf * pdev));
  505.  
  506. /* Begin an object, optionally allocating an ID. */
  507. long pdf_open_obj(P2(gx_device_pdf * pdev, long id));
  508. long pdf_begin_obj(P1(gx_device_pdf * pdev));
  509.  
  510. /* End an object. */
  511. int pdf_end_obj(P1(gx_device_pdf * pdev));
  512.  
  513. /* ------ Page contents ------ */
  514.  
  515. /* Open a page contents part. */
  516. /* Return an error if the page has too many contents parts. */
  517. int pdf_open_contents(P2(gx_device_pdf * pdev, pdf_context_t context));
  518.  
  519. /* Close the current contents part if we are in one. */
  520. int pdf_close_contents(P2(gx_device_pdf * pdev, bool last));
  521.  
  522. /* ------ Resources et al ------ */
  523.  
  524. /*
  525.  * Define the offset that indicates that a file position is in the
  526.  * asides file rather than the main (contents) file.
  527.  * Must be a power of 2, and larger than the largest possible output file.
  528.  */
  529. #define ASIDES_BASE_POSITION min_long
  530.  
  531. /* Begin an object logically separate from the contents. */
  532. /* (I.e., an object in the resource file.) */
  533. long pdf_open_separate(P2(gx_device_pdf * pdev, long id));
  534. long pdf_begin_separate(P1(gx_device_pdf * pdev));
  535.  
  536. /* Begin an aside (resource, annotation, ...). */
  537. int pdf_begin_aside(P4(gx_device_pdf * pdev, pdf_resource_t **plist,
  538.                const gs_memory_struct_type_t * pst,
  539.                pdf_resource_t **ppres));
  540.  
  541. /* Begin a resource of a given type. */
  542. int pdf_begin_resource(P4(gx_device_pdf * pdev, pdf_resource_type_t rtype,
  543.               gs_id rid, pdf_resource_t **ppres));
  544.  
  545. /* Begin a resource body of a given type. */
  546. int pdf_begin_resource_body(P4(gx_device_pdf * pdev, pdf_resource_type_t rtype,
  547.                    gs_id rid, pdf_resource_t **ppres));
  548.  
  549. /* Allocate a resource, but don't open the stream. */
  550. int pdf_alloc_resource(P5(gx_device_pdf * pdev, pdf_resource_type_t rtype,
  551.               gs_id rid, pdf_resource_t **ppres, long id));
  552.  
  553. /* Find a resource of a given type by gs_id. */
  554. pdf_resource_t *pdf_find_resource_by_gs_id(P3(gx_device_pdf * pdev,
  555.                           pdf_resource_type_t rtype,
  556.                           gs_id rid));
  557.  
  558. /* Get the object id of a resource. */
  559. long pdf_resource_id(P1(const pdf_resource_t *pres));
  560.  
  561. /* End a separate object. */
  562. int pdf_end_separate(P1(gx_device_pdf * pdev));
  563.  
  564. /* End an aside. */
  565. int pdf_end_aside(P1(gx_device_pdf * pdev));
  566.  
  567. /* End a resource. */
  568. int pdf_end_resource(P1(gx_device_pdf * pdev));
  569.  
  570. /* Copy data from a temporary file to a stream. */
  571. void pdf_copy_data(P3(stream *s, FILE *file, long count));
  572.  
  573. /* ------ Pages ------ */
  574.  
  575. /* Get or assign the ID for a page. */
  576. /* Returns 0 if the page number is out of range. */
  577. long pdf_page_id(P2(gx_device_pdf * pdev, int page_num));
  578.  
  579. /* Get the page structure for the current page. */
  580. pdf_page_t *pdf_current_page(P1(gx_device_pdf *pdev));
  581.  
  582. /* Get the dictionary object for the current page. */
  583. cos_dict_t *pdf_current_page_dict(P1(gx_device_pdf *pdev));
  584.  
  585. /* Open a page for writing. */
  586. int pdf_open_page(P2(gx_device_pdf * pdev, pdf_context_t context));
  587.  
  588. /* Write saved page- or document-level information. */
  589. int pdf_write_saved_string(P2(gx_device_pdf * pdev, gs_string * pstr));
  590.  
  591. /* ------ Path drawing ------ */
  592.  
  593. /* Test whether the clip path needs updating. */
  594. bool pdf_must_put_clip_path(P2(gx_device_pdf * pdev, const gx_clip_path * pcpath));
  595.  
  596. /* Write and update the clip path. */
  597. int pdf_put_clip_path(P2(gx_device_pdf * pdev, const gx_clip_path * pcpath));
  598.  
  599. /* ------ Miscellaneous output ------ */
  600.  
  601. #define PDF_MAX_PRODUCER 200    /* adhoc */
  602. /* Generate the default Producer string. */
  603. void pdf_store_default_Producer(P1(char buf[PDF_MAX_PRODUCER]));
  604.  
  605. /* Define the strings for filter names and parameters. */
  606. typedef struct pdf_filter_names_s {
  607.     const char *ASCII85Decode;
  608.     const char *ASCIIHexDecode;
  609.     const char *CCITTFaxDecode;
  610.     const char *DCTDecode;
  611.     const char *DecodeParms;
  612.     const char *Filter;
  613.     const char *FlateDecode;
  614.     const char *LZWDecode;
  615.     const char *RunLengthDecode;
  616. } pdf_filter_names_t;
  617. #define PDF_FILTER_NAMES\
  618.   "/ASCII85Decode", "/ASCIIHexDecode", "/CCITTFaxDecode",\
  619.   "/DCTDecode",  "/DecodeParms", "/Filter", "/FlateDecode",\
  620.   "/LZWDecode", "/RunLengthDecode"
  621. #define PDF_FILTER_NAMES_SHORT\
  622.   "/A85", "/AHx", "/CCF", "/DCT", "/DP", "/F", "/Fl", "/LZW", "/RL"
  623.  
  624. /* Write matrix values. */
  625. void pdf_put_matrix(P4(gx_device_pdf *pdev, const char *before,
  626.                const gs_matrix *pmat, const char *after));
  627.  
  628. /* Write a name, with escapes for unusual characters. */
  629. void pdf_put_name_escaped(P4(stream *s, const byte *nstr, uint size,
  630.                  bool escape));
  631. void pdf_put_name(P3(const gx_device_pdf *pdev, const byte *nstr, uint size));
  632.  
  633. /* Write a string in its shortest form ( () or <> ). */
  634. void pdf_put_string(P3(const gx_device_pdf *pdev, const byte *str, uint size));
  635.  
  636. /* Write a value, treating names specially. */
  637. void pdf_write_value(P3(const gx_device_pdf *pdev, const byte *vstr, uint size));
  638.  
  639. /* Store filters for a stream. */
  640. int pdf_put_filters(P4(cos_dict_t *pcd, gx_device_pdf *pdev, stream *s,
  641.                const pdf_filter_names_t *pfn));
  642.  
  643. /* Define a possibly encoded and compressed data stream. */
  644. typedef struct pdf_data_writer_s {
  645.     psdf_binary_writer binary;
  646.     long start;
  647.     long length_id;
  648. } pdf_data_writer_t;
  649. /*
  650.  * Begin a Function or halftone data stream.  The client has opened the
  651.  * object and written the << and any desired dictionary keys.
  652.  */
  653. int pdf_begin_data(P2(gx_device_pdf *pdev, pdf_data_writer_t *pdw));
  654.  
  655. /* End a data stream. */
  656. int pdf_end_data(P1(pdf_data_writer_t *pdw));
  657.  
  658. /* Define the maximum size of a Function reference. */
  659. #define MAX_REF_CHARS ((sizeof(long) * 8 + 2) / 3)
  660.  
  661. /* Create a Function object. */
  662. #ifndef gs_function_DEFINED
  663. typedef struct gs_function_s gs_function_t;
  664. #  define gs_function_DEFINED
  665. #endif
  666. int pdf_function(P3(gx_device_pdf *pdev, const gs_function_t *pfn,
  667.             cos_value_t *pvalue));
  668.  
  669. /* Write a Function object, returning its object ID. */
  670. int pdf_write_function(P3(gx_device_pdf *pdev, const gs_function_t *pfn,
  671.               long *pid));
  672.  
  673. /* ---------------- Exported by gdevpdfm.c ---------------- */
  674.  
  675. /*
  676.  * Define the type for a pdfmark-processing procedure.
  677.  * If nameable is false, the objname argument is always NULL.
  678.  */
  679. #define pdfmark_proc(proc)\
  680.   int proc(P5(gx_device_pdf *pdev, gs_param_string *pairs, uint count,\
  681.           const gs_matrix *pctm, const gs_param_string *objname))
  682.  
  683. /* Compare a C string and a gs_param_string. */
  684. bool pdf_key_eq(P2(const gs_param_string * pcs, const char *str));
  685.  
  686. /* Scan an integer out of a parameter string. */
  687. int pdfmark_scan_int(P2(const gs_param_string * pstr, int *pvalue));
  688.  
  689. /* Process a pdfmark (called from pdf_put_params). */
  690. int pdfmark_process(P2(gx_device_pdf * pdev, const gs_param_string_array * pma));
  691.  
  692. /* Close the current level of the outline tree. */
  693. int pdfmark_close_outline(P1(gx_device_pdf * pdev));
  694.  
  695. /* Finish writing an article. */
  696. int pdfmark_write_article(P2(gx_device_pdf * pdev, const pdf_article_t * part));
  697.  
  698. /* ---------------- Exported by gdevpdfr.c ---------------- */
  699.  
  700. /* Test whether an object name has valid syntax, {name}. */
  701. bool pdf_objname_is_valid(P2(const byte *data, uint size));
  702.  
  703. /*
  704.  * Look up a named object.  Return e_rangecheck if the syntax is invalid,
  705.  * e_undefined if no object by that name exists.
  706.  */
  707. int pdf_find_named(P3(gx_device_pdf * pdev, const gs_param_string * pname,
  708.               cos_object_t **ppco));
  709.  
  710. /*
  711.  * Create a named object.  id = -1L means do not assign an id.  pname = 0
  712.  * means just create the object, do not name it.
  713.  */
  714. int pdf_create_named(P5(gx_device_pdf *pdev, const gs_param_string *pname,
  715.             cos_type_t cotype, cos_object_t **ppco, long id));
  716. int pdf_create_named_dict(P4(gx_device_pdf *pdev, const gs_param_string *pname,
  717.                  cos_dict_t **ppcd, long id));
  718.  
  719. /*
  720.  * Look up a named object as for pdf_find_named.  If the object does not
  721.  * exist, create it (as a dictionary if it is one of the predefined names
  722.  * {ThisPage}, {NextPage}, {PrevPage}, or {Page<#>}, otherwise as a
  723.  * generic object) and return 1.
  724.  */
  725. int pdf_refer_named(P3(gx_device_pdf *pdev, const gs_param_string *pname,
  726.                cos_object_t **ppco));
  727.  
  728. /*
  729.  * Look up a named object as for pdf_refer_named.  If the object already
  730.  * exists and is not simply a forward reference, return e_rangecheck;
  731.  * if it exists as a forward reference, set its type and return 0;
  732.  * otherwise, create the object with the given type and return 1.
  733.  * pname = 0 is allowed: in this case, simply create the object.
  734.  */
  735. int pdf_make_named(P5(gx_device_pdf * pdev, const gs_param_string * pname,
  736.               cos_type_t cotype, cos_object_t **ppco, bool assign_id));
  737. int pdf_make_named_dict(P4(gx_device_pdf * pdev, const gs_param_string * pname,
  738.                cos_dict_t **ppcd, bool assign_id));
  739.  
  740. /*
  741.  * Look up a named object as for pdf_refer_named.  If the object does not
  742.  * exist, or is a forward reference, return e_undefined; if the object
  743.  * exists has the wrong type, return e_typecheck.
  744.  */
  745. int pdf_get_named(P4(gx_device_pdf * pdev, const gs_param_string * pname,
  746.              cos_type_t cotype, cos_object_t **ppco));
  747.  
  748. /*
  749.  * Scan a string for a token.  <<, >>, [, and ] are treated as tokens.
  750.  * Return 1 if a token was scanned, 0 if we reached the end of the string,
  751.  * or an error.  On a successful return, the token extends from *ptoken up
  752.  * to but not including *pscan.
  753.  */
  754. int pdf_scan_token(P3(const byte **pscan, const byte * end,
  755.               const byte **ptoken));
  756.  
  757. /*
  758.  * Scan a possibly composite token: arrays and dictionaries are treated as
  759.  * single tokens.
  760.  */
  761. int pdf_scan_token_composite(P3(const byte **pscan, const byte * end,
  762.                 const byte **ptoken));
  763.  
  764. /* Replace object names with object references in a (parameter) string. */
  765. int pdf_replace_names(P3(gx_device_pdf *pdev, const gs_param_string *from,
  766.              gs_param_string *to));
  767.  
  768. #endif /* gdevpdfx_INCLUDED */
  769.